﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.Qymd_TransManage.Model;
using FastReport;
using System.Xml;
using ZK.Common;
using System.IO;

namespace ZK.Qymd_TransManage
{
    public partial class SalesRecords : Form
    {
        private List<DataGridModel> FBusinessObject;
        List<DataGridModel> ListDataModel = new List<DataGridModel>();
        DataTable myTable = new DataTable();
        public SalesRecords()
        {
            InitializeComponent();
            label4.Text = "当前操作员:" + Login.name;
            //aspnetPager1.OnPageChanged += AspnetPager1_OnPageChanged;
        }
        private void AspnetPager1_OnPageChanged(object sender, EventArgs e)
        {
            GetData();
        }
        int total = 0;
        private void GetData()
        {
            try
            {
                ServiceReferenceApi.AccessApiServiceClient client = new ServiceReferenceApi.AccessApiServiceClient();
                ListDataModel = new List<DataGridModel>();
                ServiceReferenceApi.TruckInfoDto model = new ServiceReferenceApi.TruckInfoDto();
                model.车牌号 = txtUsername.Text;
                model.起始时间 = dtStart.Text;
                model.结束时间 = dtEnd.Text;
                Common.Log4.LogManage.WriteInfo(Newtonsoft.Json.JsonConvert.SerializeObject(model));
                ServiceReferenceApi.ApiLoadingResponseDto bill = client.GetTruckInfo(model);
                Common.Log4.LogManage.WriteInfo(Newtonsoft.Json.JsonConvert.SerializeObject(bill));
                //DataGridModel dataModel = null;
                dt = bill.dt;
                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataGridModel dataModel = new DataGridModel();
                        dataModel.TruckNum = dr["车号"].ToString();
                        if (dr["毛重"].ToString() != "" && dr["毛重"].ToString() != null)
                        {
                            dataModel.GrossWeight = dr["毛重"].ToString();
                        }
                        if (dr["皮重"].ToString() != "" && dr["皮重"].ToString() != null)
                        {
                            dataModel.Tare = dr["皮重"].ToString();
                        }
                        if (dr["净重"].ToString() != "" && dr["净重"].ToString() != null)
                        {
                            dataModel.NetWeight = dr["净重"].ToString();
                        }
                        dataModel.CoalType = dr["货物名称"].ToString();
                        dataModel.ShipperName = dr["发货单位"].ToString();
                        dataModel.ReceivingPartyName = dr["收货单位"].ToString();
                        dataModel.TransportUnit = dr["运输单位"].ToString();
                        dataModel.GrossWeightTime = dr["毛重时间"].ToString();
                        dataModel.TareTime = dr["皮重时间"].ToString();
                        dataModel.DeleteSign = dr["作废标志"].ToString();
                        if (dataModel.DeleteSign == "true")
                        {
                            dataModel.DeleteSign = "是";
                        }
                        else
                        {
                            dataModel.DeleteSign = "否";
                        }
                        dataModel.GrossWeightUser = dr["毛重计量员"].ToString();
                        dataModel.TareUser = dr["皮重计量员"].ToString();
                        dataModel.WeighDate = dr["称重日期"].ToString();
                        if (dataModel.NetWeight != "0")
                        {
                            ListDataModel.Add(dataModel);
                        }

                    }
                    dgView.AutoGenerateColumns = false;
                    dgView.DataSource = ListDataModel;
                    total = dt.Rows.Count;
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询异常！");
                Common.Log4.LogManage.WriteInfo(ex.ToString());
            }

        }

        DataTable dt = null;
        private void btnSearchInfo_Click(object sender, EventArgs e)
        {
            GetData();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            int i = 0;
            if (dgView.CurrentRow.Index.ToString() != null)
            {
                i = dgView.CurrentRow.Index;
            }
            DataGridModel bill = new DataGridModel();

            if (dgView.Rows[i].Selected == true)
            {
                if (dt.Rows.Count != 0)
                {
                    bill.TruckNum = dt.Rows[i]["车号"].ToString();
                    if (dt.Rows[i]["毛重"].ToString() != "" && dt.Rows[i]["毛重"].ToString() != null)
                    {
                        bill.GrossWeight = dt.Rows[i]["毛重"].ToString();
                    }
                    if (dt.Rows[i]["皮重"].ToString() != "" && dt.Rows[i]["皮重"].ToString() != null)
                    {
                        bill.Tare = dt.Rows[i]["皮重"].ToString();
                    }
                    if (dt.Rows[i]["净重"].ToString() != "" && dt.Rows[i]["净重"].ToString() != null)
                    {
                        bill.NetWeight = dt.Rows[i]["净重"].ToString();
                    }
                    bill.CoalType = dt.Rows[i]["货物名称"].ToString();
                    bill.ShipperName = dt.Rows[i]["发货单位"].ToString();
                    bill.ReceivingPartyName = dt.Rows[i]["收货单位"].ToString();
                    bill.TransportUnit = dt.Rows[i]["运输单位"].ToString();
                    bill.GrossWeightTime = dt.Rows[i]["毛重时间"].ToString();
                    bill.TareTime = dt.Rows[i]["皮重时间"].ToString();
                    bill.DeleteSign = dt.Rows[i]["作废标志"].ToString();
                    bill.GrossWeightUser = txtConsignee.Text.ToString();
                    bill.TareUser = dt.Rows[i]["皮重计量员"].ToString();
                    bill.WeighDate = dt.Rows[i]["称重日期"].ToString();
                    bill.name = Login.name;
                    XmlDocument document = new XmlDocument();
                    document.Load("UserData.xml");
                    XmlNode node = document.SelectSingleNode("/users/data");
                    if (node != null)
                    {
                        string date1 = node.SelectSingleNode("date").InnerText;
                        string num = node.SelectSingleNode("num").InnerText;
                        string nowdate = DateTime.Now.ToString("yyyyMMdd");
                        if (date1 == nowdate)
                        {
                            bill.TicketNum = date1 + num;
                            int n = int.Parse(num) + 1;
                            node.SelectSingleNode("num").InnerText = n.ToString();
                            document.Save("UserData.xml");
                        }
                        else if (date1 != nowdate)
                        {
                            node.SelectSingleNode("date").InnerText = nowdate;
                            node.SelectSingleNode("num").InnerText = "1";
                            document.Save("UserData.xml");
                            bill.TicketNum = nowdate + 1;
                        }
                    }
                }
            }
            FBusinessObject = new List<DataGridModel>();
            FBusinessObject.Add(bill);
            Report report = new Report();
            report.Load(@"QYMDBD.frx");
            report.RegisterData(FBusinessObject, "DataTable");
            //预览打印
            report.Show();

            report.Dispose();
            //静默打印
            //report.PrintSettings.ShowDialog = false;
            // report.Print();
            //report.Dispose();

            //创建fastreport模版
            //Report report = new Report();

            //report.RegisterData(FBusinessObject, "DataTable");

            //report.Design();

            //report.Dispose();

        }

        private void SalesRecords_Load(object sender, EventArgs e)
        {
            ////页面大小
            //aspnetPager1.PageSize = 20;
            ////当前页
            //aspnetPager1.PageIndex = 1;
        }
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void kryptonButton1_Click(object sender, EventArgs e)
        {

            FolderBrowserDialog dialog = new FolderBrowserDialog();//弹出选择文件路径
            dialog.Description = "请选择文件路径";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                //DirectoryInfo theFolder = new DirectoryInfo(foldPath);

                ////theFolder 包含文件路径

                //FileInfo[] dirInfo = theFolder.GetFiles();
                ////遍历文件夹                
                //foreach (FileInfo file in dirInfo)
                //{
                //    MessageBox.Show(file.ToString());
                //}
                string path = foldPath + @"\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv";//拼接路径
                #region 表头
                string cTitle = "车辆过磅信息";
                List<string> cName = new List<string>();
                cName.Add("车牌号");
                cName.Add("毛重");
                cName.Add("皮重");
                cName.Add("净重");
                cName.Add("货物名称");
                cName.Add("发货单位");
                cName.Add("收货单位");
                cName.Add("运输单位");
                cName.Add("毛重时间");
                cName.Add("皮重时间");
                cName.Add("作废标志");
                cName.Add("毛重计量员");
                cName.Add("称重日期");
                #endregion
                // ListDataModel.Add(new DataGridModel() { TruckNum = "asdqwe" });//数据源
                if (Com.DataToExcel.dt2csv(Com.DataToExcel.ToDataTable(ListDataModel), path, cTitle, string.Join(",", cName)))
                {
                    MessageBox.Show("导出成功");
                }
                else
                {
                    MessageBox.Show("导出失败");
                }
            }



        }

    }

    public class AA
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
